package com.seth.myapplication.surface;

import android.os.Bundle;
import android.support.v4.app.Fragment;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.seth.myapplication.R;

import android.widget.ScrollView;

import com.seth.myapplication.adapter.BlockAdapter;
import com.seth.myapplication.adapter.ItemAdapter;
import com.seth.myapplication.helper.ItemTouchHelperCallback;

import com.seth.myapplication.tools.GlideImageLoader;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Seth on 2017/9/27.
 */
public class page1Fragment extends Fragment {
    private RecyclerView mRecyclerView;
    private List<String> infos = new ArrayList<>();
   // private List<String> blockInfo = new ArrayList<>();
    private ItemAdapter mMyAdapter;
   // private BlockAdapter mBlockAdapter;
    private View view;
    private static ScrollView myScrollView;
    private ItemTouchHelper helper;
    private  LinearLayoutManager linearLayoutManager;

    private Banner banner;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        view = inflater.inflate(R.layout.first_page_fragment,container,false);

        initView();
        initEvent();

        return view;
    }

    private void initEvent() {
        initData();

        //blockRecyclerView.setLayoutManager(new GridLayoutManager(getContext(),4));
        //blockRecyclerView.setAdapter(mBlockAdapter);
       // ItemTouchHelper.Callback callback = new ItemTouchHelperCallback(mBlockAdapter);
       // helper = new ItemTouchHelper(callback);
        //helper.attachToRecyclerView(blockRecyclerView);
        /**
         * 下面是内容展示RecyclerView，
         * 这里主要为了使ScrollView整体滑动以及item能够在ScrollView下实现match_parent
         * 因此并未绑定拖拽事件
         */
        linearLayoutManager = new LinearLayoutManager(getContext()){
            /**
             * 返回false禁止RecyclerView竖直滚动
             * @return
             */
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };

        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(mMyAdapter);
        updateBanner();
        setScrollView();
    }

    private void initView() {
        myScrollView = (ScrollView) view.findViewById(R.id.scrollView);
        banner = (Banner) view.findViewById(R.id.ads_banner);
        mRecyclerView = (RecyclerView)view.findViewById(R.id.inf_show);
        //blockRecyclerView = (RecyclerView) view.findViewById(R.id.block_view);
    }

    public static void setScrollView(){

        myScrollView.smoothScrollTo(0,20);
    }

    private void initData() {

        for(int i=1; i<100; ++i){
            String itemText = "";
            itemText = "帮我去弗雷德地下一层拿" + (i+1) + "个快递";
            infos.add(itemText);
           // if(i<5)
            //    blockInfo.add("标签"+ i);

        }
        mMyAdapter = new ItemAdapter(getActivity(), infos);
        //mBlockAdapter = new BlockAdapter(getActivity(),blockInfo);


    }

    private void updateBanner() {
        //加载图片资源
        Integer[] images={R.mipmap.banner1,R.mipmap.banner2,R.mipmap.banner3,R.mipmap.banner4};
        List<Integer> img  = new ArrayList<>();
        for(Integer s : images){
            img.add(s);
        }
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集和
        banner.setImages(img);
        //自动轮播
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(5000);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }
}
